// pages/friends/index.ts
import tran from "../../../utils/language";
import { userChatStatistics } from "../../../utils/api";
Page({
    /**
     * 页面的初始数据
     */
    data: {
        _t: tran._t("participationStatistics"),
        _tBase: tran._t("base"),
        navBarHeight: getApp().globalData.navBarHeight,
        swiperIndex: 0,
        // 设置页面
        imgList: [
            {
                deviceImg: "/images/purpleMbti.png",
                isComplete: false,
                personalityType: 1, // 1紫色  2绿色  3蓝色  4黄色
            },
            {
                deviceImg: "/images/greenMbti.png",
                isComplete: false,
                personalityType: 2, // 1紫色  2绿色  3蓝色  4黄色
            },
            {
                deviceImg: "/images/blueMbti.png",
                isComplete: false,
                personalityType: 4, // 1紫色  2绿色  3蓝色  4黄色
            },
            {
                deviceImg: "/images/yellowMbti.png",
                isComplete: false,
                personalityType: 3, // 1紫色  2绿色  3蓝色  4黄色
            },
        ],
        mbtiList: [
            {
                personality: "",
                chatCount: 0,
            },
        ],
        purpleList: [
            {
                personality: "INTJ",
                chatCount: 0,
            },
            {
                personality: "INTP",
                chatCount: 0,
            },
            {
                personality: "ENTJ",
                chatCount: 0,
            },
            {
                personality: "ENTP",
                chatCount: 0,
            },
        ],
        greenList: [
            {
                personality: "INFJ",
                chatCount: 0,
            },
            {
                personality: "INFP",
                chatCount: 0,
            },
            {
                personality: "ENFJ",
                chatCount: 0,
            },
            {
                personality: "ENFP",
                chatCount: 0,
            },
        ],
        blueList: [
            {
                personality: "ISTJ",
                chatCount: 0,
            },
            {
                personality: "ISFJ",
                chatCount: 0,
            },
            {
                personality: "ESTJ",
                chatCount: 0,
            },
            {
                personality: "ESFJ",
                chatCount: 0,
            },
        ],
        yellowList: [
            {
                personality: "ISTP",
                chatCount: 0,
            },
            {
                personality: "ISFP",
                chatCount: 0,
            },
            {
                personality: "ESTP",
                chatCount: 0,
            },
            {
                personality: "ESFP",
                chatCount: 0,
            },
        ],
        chatList: [
            {
                personality: "",
                chatCount: "",
            },
        ],
        allNoPurpleList: ["INTJ", "INTP", "ENTJ", "ENTP"],
        allNoGreenList: ["INFJ", "ENFP", "INFP", "ENFJ"],
        allNoBlueList: ["ESTJ", "ESFJ", "ISTJ", "ISFJ"],
        allNoYellowList: ["ISTP", "ISFP", "ESTP", "ESFP"],
        noPurpleList: ["INTJ", "INTP", "ENTJ", "ENTP"],
        noGreenList: ["INFJ", "ENFP", "INFP", "ENFJ"],
        noBlueList: ["ESTJ", "ESFJ", "ISTJ", "ISFJ"],
        noYellowList: ["ISTP", "ISFP", "ESTP", "ESFP"],
        purplePic: "",
        greenPic: "",
        yellowPic: "",
        bluePic: "",
        mbtiText: "",
        personalityType: 2, // 1紫色  2绿色  3蓝色  4黄色
        overNumList: [0, 0, 0, 0],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        this.getChat();
    },
    handleMbtiText() {
        const {
            noPurpleList,
            noGreenList,
            noBlueList,
            noYellowList,
            swiperIndex,
        } = this.data;
        let mbtiText = "";
        console.log(swiperIndex);

        if (swiperIndex == 0) {
            // 紫色
            mbtiText = `紫队中${noPurpleList.join("、")}`;
        } else if (swiperIndex == 1) {
            // 绿色
            mbtiText = `绿队中${noGreenList.join("、")}`;
        } else if (swiperIndex == 3) {
            // 黄色
            mbtiText = `黄队中${noYellowList.join("、")}`;
        } else if (swiperIndex == 2) {
            // 蓝色
            mbtiText = `蓝队中${noBlueList.join("、")}`;
        }
        this.setData({
            mbtiText,
        });
    },
    getChat() {
        let {
            purpleList,
            greenList,
            blueList,
            yellowList,
            noPurpleList,
            noGreenList,
            noBlueList,
            noYellowList,
            allNoGreenList,
            allNoPurpleList,
            allNoBlueList,
            allNoYellowList,
            overNumList,
            imgList,
        } = this.data;
        const params = {};
        userChatStatistics(params).then((res) => {
            if (res.length > 0) {
                noPurpleList = [];
                noGreenList = [];
                noBlueList = [];
                noYellowList = [];
            }
            // 过滤出每种类型有几个，然后计算完成度，一个就是25,两个就是50
            let purpleNum = res.filter(
                (item: any) =>
                    item.personality == "INTJ" ||
                    item.personality == "INTP" ||
                    item.personality == "ENTJ" ||
                    item.personality == "ENTP"
            ).length;
            let greenNum = res.filter(
                (item: any) =>
                    item.personality == "INFJ" ||
                    item.personality == "ENFP" ||
                    item.personality == "INFP" ||
                    item.personality == "ENFJ"
            ).length;
            let blueNum = res.filter(
                (item: any) =>
                    item.personality == "ESTJ" ||
                    item.personality == "ESFJ" ||
                    item.personality == "ISTJ" ||
                    item.personality == "ISFJ"
            ).length;
            let yellowNum = res.filter(
                (item: any) =>
                    item.personality == "ISTP" ||
                    item.personality == "ISFP" ||
                    item.personality == "ESTP" ||
                    item.personality == "ESFP"
            ).length;
            overNumList = [
                (purpleNum || 0) * 25,
                (greenNum || 0) * 25,
                (blueNum || 0) * 25,
                (yellowNum || 0) * 25,
            ];
            let purplePic = `/images/purple${purpleNum || 0}.png`;
            let greenPic = `/images/green${greenNum || 0}.png`;
            let yellowPic = `/images/yellow${blueNum || 0}.png`;
            let bluePic = `/images/blue${yellowNum || 0}.png`;
            imgList.map((item: any) => {
                if (item.personalityType == 1) {
                    // 紫色
                    item.deviceImg = purplePic;
                    item.isComplete = purpleNum == 4 ? true : false;
                }
                if (item.personalityType == 2) {
                    // 绿色
                    item.deviceImg = greenPic;
                    item.isComplete = greenNum == 4 ? true : false;
                }
                if (item.personalityType == 3) {
                    // 黄色
                    item.deviceImg = yellowPic;
                    item.isComplete = yellowNum == 4 ? true : false;
                }
                if (item.personalityType == 4) {
                    // 蓝色
                    item.deviceImg = bluePic;
                    item.isComplete = blueNum == 4 ? true : false;
                }
            });
            res.length > 0 &&
                res.forEach((item: any) => {
                    // 紫色人格
                    if (
                        item.personality == "INTJ" ||
                        item.personality == "INTP" ||
                        item.personality == "ENTJ" ||
                        item.personality == "ENTP"
                    ) {
                        console.log(purpleList);
                        purpleList.map((item2) => {
                            if (item.personality == item2.personality) {
                                // 代表有数据
                                item2.chatCount = item.chatCount;
                            } else {
                                // 代表没数据，为0
                                noPurpleList.push(item2.personality);
                            }
                        });
                    }
                    if (
                        item.personality == "INFJ" ||
                        item.personality == "ENFP" ||
                        item.personality == "INFP" ||
                        item.personality == "ENFJ"
                    ) {
                        // 绿色
                        greenList.map((item2) => {
                            if (item.personality == item2.personality) {
                                item2.chatCount = item.chatCount;
                            } else {
                                // 代表没数据，为0
                                noGreenList.push(item2.personality);
                            }
                        });
                    }
                    if (
                        item.personality == "ESTJ" ||
                        item.personality == "ESFJ" ||
                        item.personality == "ISTJ" ||
                        item.personality == "ISFJ"
                    ) {
                        // 蓝色
                        blueList.map((item2) => {
                            if (item.personality == item2.personality) {
                                item2.chatCount = item.chatCount;
                            } else {
                                // 代表没数据，为0
                                noBlueList.push(item2.personality);
                            }
                        });
                    }
                    if (
                        item.personality == "ISTP" ||
                        item.personality == "ISFP" ||
                        item.personality == "ESTP" ||
                        item.personality == "ESFP"
                    ) {
                        // 黄色
                        yellowList.map((item2) => {
                            if (item.personality == item2.personality) {
                                item2.chatCount = item.chatCount;
                            } else {
                                // 代表没数据，为0
                                noYellowList.push(item2.personality);
                            }
                        });
                    }
                });
            noPurpleList = noPurpleList.length ? noPurpleList : allNoPurpleList;
            noGreenList = noGreenList.length ? noGreenList : allNoGreenList;
            noBlueList = noBlueList.length ? noBlueList : allNoBlueList;
            noYellowList = noYellowList.length ? noYellowList : allNoYellowList;
            this.setData(
                {
                    mbtiText: `紫队中${noPurpleList}`,
                    noPurpleList,
                    noGreenList,
                    noBlueList,
                    noYellowList,
                    chatList: res || [],
                    mbtiList: purpleList,
                    purpleList,
                    greenList,
                    blueList,
                    yellowList,
                    overNumList,
                    purplePic,
                    greenPic,
                    yellowPic,
                    bluePic,
                    imgList,
                },
                () => {
                    this.handleMbtiText();
                }
            );
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (typeof this.getTabBar === "function" && this.getTabBar()) {
            this.getTabBar().setData({
                activeKey: "metrics",
            });
        }
    },
    swiper(e: any) {
        let that = this;
        const { purpleList, greenList, blueList, yellowList } = this.data;
        let index = e.detail.current;
        console.log(index);

        let list: any = [];
        if (index == 0) {
            // 紫色人格
            list = purpleList;
        } else if (index == 1) {
            // 绿色人格
            list = greenList;
        } else if (index == 2) {
            // 蓝色人格
            list = blueList;
        } else if (index == 3) {
            // 黄色人格
            list = yellowList;
        }
        console.log(list);

        that.setData(
            {
                swiperIndex: index,
                mbtiList: list,
            },
            () => {
                this.handleMbtiText();
            }
        );
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
});
